#include "head.h"

int do_login(int sockfd, MSG *pbuf)
{
	pbuf->type = LOGIN;
   puts("--------------login--------------");

   printf("input your name:");
   scanf("%s", pbuf->name);
   getchar();

	printf("input password:");
	scanf("%s", pbuf->passwd);
	getchar();

	send(sockfd, pbuf, sizeof(MSG), 0);

	recv(sockfd, pbuf, sizeof(MSG), 0);

	return pbuf->ret;
}

void do_stuff(int sockfd, MSG *pbuf)
{
	int mode;

	puts("welcome, %s, please choose operation mode\n", pbuf->name);
	puts("1.show your own information    2.change your own password");
	puts("3.change your own information");

	scanf("%d", &mode);

	switch(mode)
	{
	case 1://显示个人信息
		{
			pbuf->type = STAFF_QUERY;
			send(sockfd, pbuf, sizeof(MSG), 0);

			recv(sockfd, pbuf, sizeof(MSG), 0);

			puts("id	name	addr	age		salary");
			printf("%d ", pbuf->id);
			printf("%s ", pbuf->name);
			printf("%s ", pbuf->addr);
			printf("%d ", pbuf->age);
			printf("%f\n", pbuf->salary);
		}
		break;

	case 2://改变个人密码
		{
			pbuf->type = STAFF_CHANGE_PASSWD;

			puts("input new password");
			scanf("%s", pbuf->passwd);

			send(sockfd, pbuf, sizeof(MSG), 0);

			recv(sockfd, pbuf, sizeof(MSG), 0);
			printf("%s\n", pbuf->data);
		}
		break;

	case 3:更改个人信息
		{
			puts("");
		}
		break;

		default:
			puts("input error");
		break;
	}


}

int do_admin(int sockfd, MSG *pbuf)
{
	int mode;
    printf("welcome, %s, please choose operation mode \n", pbuf->name);
	puts("1.show all staff  2.change password  3.change staff information");
	puts("4.add staff  5.delete staff");

	scanf("%d", &mode);
	getchar();

	switch(mode)
	{
		case 1://显示所有的员工
			{
				pbuf.type = ADMIN_QUERY;

				send(sockfd, pbuf, sizeof(MSG), 0);

				recv(sockfd, pbuf, sizeof(MSG), 0);

				printf("id     name     addr    age    salary");
				while(1)
				{
					printf("%d ", pbuf->info.id);
					printf("%s ", pbuf->info.name);
					pritnf("%s ", pbuf->info.addr);
					printf("%d ", pbuf->info.age);
					printf("%f\n", pbuf->info.salary);

					if(recv(sockfd, pbuf, sizeof(MSG), 0) <= 0)
						break;
				}
				
			}
			break;

		case 2://改变密码
			{
				pbuf->type = STAFF_CHANGE_PASSWD;
				puts("input your new password");
				scanf("%s", pbuf->passwd);

				send(sockfd, pbuf, sizeof(MSG), 0);
				recv(sockfd, pbuf, sizeof(MSG), 0);
				printf("%s\n", pbuf->data);
			}
			break;

		case 3://改变员工信息
			{
				puts("please input id to change ");
			}
			break;

		case 4://添加员工
			{
				printf("input id:");
				scanf("%d", pbuf->info.id);
				getchar();

				printf("input name:");
				scanf("%s", pbuf->info.name);
				getchar();

				printf("input addr:");
				scanf("%s", pbuf->info.addr);
				getchar();

				printf("input age:");
				scanf("%d", pbuf->info.age);
				getchar();

				printf("input salary:");
				scanf("%f", pbuf->info.salary);
				getchar();

				send(sockfd, pbuf, sizeof(MSG), 0);

				recv(sockfd, pbuf, sizeof(MSG), 0);

				printf("%s\n", pbuf->data);
			}
			break;

		case 5://删除员工
			{
				puts("input id to delete ");
				printf("input id:");
				scanf("%d", pbuf->info.id);
				getchar();
				
				send(sockfd, pbuf, sizeof(MSG), 0);	

				recv(sockfd, pbuf, sizeof(MSG), 0);
				printf("%s\n", pbuf->data);

			}
			break;
			
		default:
			puts("input error");
			break;

	}

}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    char clean[64];

    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    //连接服务器
    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("fail to connect");
        exit(-1);
    }
	else
	{
		puts("connect succeed");
	}

    ret = do_login(sockfd, &buf);
    if(ret == STAFF)
	{  //员工
        puts("stuff login");
        do_stuff(sockfd, &buf);
    }
	else
	{  //管理员
        puts("admin login");
        do_admin(sockfd, &buf);
    }
}
